<?php

namespace App\Http\Controllers\Admin;


use App\Http\Model\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;
use Mews\Captcha\Captcha;

class LoginController extends CommonController
{
    //
    public function login(Request $request)
    {
        if($request->method() == 'POST')
        {
            $rules = ['code' => 'required|captcha'];
            $validator = \Validator::make(Input::all(), $rules);
            if ($validator->fails())
            {
                return back()->with('msg','验证码错误');
            }
            $input = Input::all();
            $user = User::first();

            if($user->user_name != $input['username'] || Crypt::decrypt($user->user_pass) != $input['password'])
            {
                return back()->with('msg','用户名或密码错误');
            }
            session(['user'=>$user]);

            return redirect('admin/index');
        }
        else
        {
            return view('admin.login');
        }
    }

    /**
     * 产生验证码
     * @return Captcha
     * create by wenQing
     */
    public function code()
    {
        return captcha();
    }

    public function crypt()
    {
        $str = '123456';
        $str1 = Crypt::encrypt($str);
        echo $str1;
        //echo Crypt::decrypt($str1);
    }
}
